package org.codegallery.javagal.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class App {
	ExecutorService executor;
	ArchiveSearcher searcher;
	public void showFuture(final String target) throws InterruptedException{
		Future<String> future
	       = executor.submit(new Callable<String>() {
	         public String call() {
	             return searcher.search(target);
	         }});
		
	     FutureTask<String> ft =
	         new FutureTask<String>(new Callable<String>() {
	           public String call() {
	             return searcher.search(target);
	         }});
	       executor.execute(ft);
	       
	     displayOtherThings(); // do other things while searching
	     try {
	       displayText(future.get()); // use future
	     } catch (ExecutionException ex) { cleanup(); return; }
	}
	
	public void displayOtherThings(){
		
	}
	
	public void displayText(String s){
		
	}
	
	void shutdownAndAwaitTermination(ExecutorService pool) {
		   pool.shutdown(); // Disable new tasks from being submitted
		   try {
		     // Wait a while for existing tasks to terminate
		     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
		       pool.shutdownNow(); // Cancel currently executing tasks
		       // Wait a while for tasks to respond to being cancelled
		       if (!pool.awaitTermination(60, TimeUnit.SECONDS))
		           System.err.println("Pool did not terminate");
		     }
		   } catch (InterruptedException ie) {
		     // (Re-)Cancel if current thread also interrupted
		     pool.shutdownNow();
		     // Preserve interrupt status
		     Thread.currentThread().interrupt();
		   }
		 }
		 
	
	interface ArchiveSearcher { String search(String target); }
	
	public void cleanup(){
		
	}

}
